package com.sch.www.ele.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.codeGenerator.base.CustomPage;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.ele.entity.Elevator;
import com.sch.www.ele.services.ElevatorServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("v1/elevator")
@RestController
public class ElevatorController extends BaseController {


    @Autowired
    ElevatorServices elevatorServices;


    @RequestMapping(value = "list")
    private ResultBean<CustomPage<Elevator>> list(
            String registrationCode,
            String elevatorNorms,
            int pageNum,
            int pageSize) {
        ResultBean<CustomPage<Elevator>> resultBean = ResultBean.newInstance();
        CustomPage<Elevator> page = elevatorServices.listByExample(registrationCode, elevatorNorms, pageNum, pageSize);
        resultBean.setSuccess().setData(page);
        return resultBean;
    }


    @PostMapping("add")
    public ResultBean<Elevator> add(@RequestBody Elevator elevator) {
        ResultBean<Elevator> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
//                .on(elevator.getName(), new NotBlankFluentValidator("name"))
//                .on(elevator.getCreditCode(), new NotBlankFluentValidator("creditCode"))
//                .on(elevator.getLiaison(), new NotBlankFluentValidator("liaison"))
//                .on(elevator.getTelephone(), new NotBlankFluentValidator("telephone"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        elevatorServices.insertSelective(elevator);
        return resultBean;
    }

    @PostMapping("modify")
    public ResultBean<Elevator> modify(@RequestBody Elevator elevator) {
        ResultBean<Elevator> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
//                .on(elevator.getName(), new NotBlankFluentValidator("name"))
//                .on(elevator.getCreditCode(), new NotBlankFluentValidator("creditCode"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        elevatorServices.updateByIdSelective(elevator);
        return resultBean;
    }


}
